package bairui.international.weixin.portal.miniapp.controller.index.module;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.querydsl.QSort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.model.uml.model.status.State_UpDown;
import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;

import bairui.international.model.domain.jpa.TB_IndexModule;
import bairui.international.model.domain.jpa.TB_IndexModuleImg;
import bairui.international.model.domain.jpa.TB_IndexModuleProduct;
import bairui.international.model.type.Type_IndexModule;
import bairui.international.service.Service_IndexModule;
import bairui.international.service.Service_IndexModuleImg;
import bairui.international.service.Service_IndexModuleProduct;
import bairui.international.weixin.portal.miniapp.controller.index.module.C_Index_Module_List_Get.Output;

/**
 *
 * 分页模块列表
 *
 * @generated
 * 
 * @see STest_C_Index_Module_PageList_Get 测试
 *
 */
@Controller
public class C_Index_Module_PageList_Get
 extends SC_Index_Module_PageList_Get 
{
	@Autowired private Service_IndexModule service_IndexModule;
	@Autowired private Service_IndexModuleProduct service_IndexModuleProduct;
	@Autowired private Service_IndexModuleImg service_IndexModuleImg;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@GetMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		@RequestParam Integer startPage,
		ModelMap map, HttpServletRequest request
	) throws Exception {
		TB_IndexModule module = service_IndexModule.findOne(
				it->it.state.eq(State_UpDown.Uped)
					.and(it.pageFlag.isTrue())
			);
		if(null == module){
			return null;
		}
		int size = 4;
		//
		Type_IndexModule type = module.getType();
		if(type == Type_IndexModule.Img){
			List<TB_IndexModuleImg> imgs = service_IndexModuleImg.page(
					it2->it2.indexModule.eq(module).and(it2.state.eq(State_UpDown.Uped)),
					PageRequest.of(startPage, size),
					it2->new QSort(it2.orderValue.desc())
				).getContent();
			return new Output(Type_IndexModule.Img, module.getName(), module.getComment(), module.getTopFlag(), imgs);
		}else if(type == Type_IndexModule.Product){
			List<TB_IndexModuleProduct> products = service_IndexModuleProduct.page(
					it2->it2.indexModule.eq(module).and(it2.state.eq(State_UpDown.Uped)),
					PageRequest.of(startPage, size),
					it2->new QSort(it2.orderValue.desc())
				).getContent();
			return new Output(Type_IndexModule.Product, module.getName(), module.getComment(), module.getTopFlag(), products);
		}else {
			throw new RuntimeException();
		}
		//
//		
//		return modules
//				.stream()
//				.map(it->{
//					
//				})
//				.collect(Collectors.toList());
	}
	
}
